package com.app.mingluxing.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.adapter.ChooseAdapter;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.RequestSimpleBean;
import com.app.mingluxing.bean.BindChildList;
import com.app.mingluxing.utils.Constant;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.LogUtil;
import com.app.mingluxing.widget.GridViewForScrollView;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 选择宝宝页面
 * Created by Aaron on 15/7/30.
 */
public class ChooseBabyActivity extends BaseActivity {

    private ChooseAdapter chooseAdapter;
    private GridViewForScrollView gv_baby;
    private List<BindChildList.Data> datas;
    private static final int CHOOSE_BABY_CODE = 10001;
    private TextView tv_left;
    private ArrayList<String> ids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_baby);
        datas = new ArrayList<BindChildList.Data>();

        ids = getIntent().getStringArrayListExtra("ids");

        initWidget();

        //获取孩子列表
        getChildList();

    }

    private void initWidget() {

        tv_left = (TextView) findViewById(R.id.tv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        TextView tv_right = (TextView) findViewById(R.id.tv_right);

        tv_left.setText(getResources().getString(R.string.select_all));
        tv_title.setText(getResources().getString(R.string.choose));
        tv_right.setText(getResources().getString(R.string.done));

        gv_baby = (GridViewForScrollView) findViewById(R.id.gv_baby);

        tv_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);

        chooseAdapter = new ChooseAdapter(this, datas);
        gv_baby.setAdapter(chooseAdapter);


        gv_baby.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //反选 收件人
//                ((People) chooseAdapter.getItem(position)).isSelect =  (((People) chooseAdapter.getItem(position)).isSelect) == 0 ? 1 : 0;
//                ((BindChildList.Data) chooseAdapter.getItem(position)).isSelect = !((BindChildList.Data) chooseAdapter.getItem(position)).isSelect;

                if (datas == null || datas.size() == 0)
                    return;

                datas.get(position).isSelect = !datas.get(position).isSelect;

                chooseAdapter.notifyDataSetChanged();
            }
        });


    }

    /**
     * 获取孩子列表
     */
    private void getChildList() {

        RequestSimpleBean requestSimpleBean = new RequestSimpleBean();
        requestSimpleBean.token = sp.getString(Constant.TOKEN, "");

        HttpUtil.setPostRequest("获取孩子列表中...", this, Constant.WEB_ROOT + Constant.GETCHILDERN, JSONObject.toJSONString(requestSimpleBean),
                getClass().getName(), new HttpUtil.SuccessListener() {
            @Override
            public void setOnSuccessListener(String str) {
                try {

                    BindChildList bindChildList = JSONObject.parseObject(str, BindChildList.class);

                    if (bindChildList.code == Constant.STATUS_SUCCESS) {
                        datas = bindChildList.children;

                        if (datas == null || datas.size() == 0) {
                            showToast("请先绑定孩子");
                            return;
                        }

                        for (int i = 0; i < datas.size(); i++) { //已选中的孩子设置 默认选中状态

                            if (ids!=null && ids.contains(datas.get(i).id)) {
                                datas.get(i).isSelect = true;
                            }

                        }

                        chooseAdapter = new ChooseAdapter(ChooseBabyActivity.this, datas);
                        gv_baby.setAdapter(chooseAdapter);

                    } else {
                        showToast(Constant.showErrorMsg(bindChildList.code, ChooseBabyActivity.this));
                    }

                } catch (Exception e) {

                    showToast(Constant.NODATA);
                    e.printStackTrace();
                    LogUtil.e("TAG", "获取学校孩子联系人数据解析错误");

                }
            }
        }, null);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        mApplication.cancelPendingRequests(getClass().getName());
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {

            case R.id.tv_left://全选或者全不选

                if (datas == null || datas.size() == 0)
                    return;

                if (tv_left.getText().toString().trim().equals(getResources().getString(R.string.select_all))) {
                    //全选

                    for (BindChildList.Data p : datas) {

                        p.isSelect= true;

                    }
                    tv_left.setText(getResources().getString(R.string.unselect_all));
                }else if (tv_left.getText().toString().trim().equals(getResources().getString(R.string.unselect_all))) {
                    //全不选

                    for (BindChildList.Data p : datas) {

                        p.isSelect= false;

                    }
                    tv_left.setText(getResources().getString(R.string.select_all));

                }

                chooseAdapter.notifyDataSetChanged();

                break;
            case R.id.tv_right://完成

                if (datas == null || datas.size() == 0){
                    showToast("请选择宝宝");
                    return;
                }

                List<BindChildList.Data> chooseedPeoples = new ArrayList<BindChildList.Data>();

                for (BindChildList.Data p : datas) {
                    if (p.isSelect) {
                        chooseedPeoples.add(p);
                    }
                }
                Intent data = new Intent();
                data.putExtra("children", (Serializable) chooseedPeoples);
                setResult(CHOOSE_BABY_CODE, data);
                finish();
                break;
        }

    }

    public void onResume() {
        super.onResume();
//        MobclickAgent.onResume(this);       //统计时长
    }

    public void onPause() {
        super.onPause();
//        MobclickAgent.onPause(this);
    }

}
